package com.eightbears.beboy.ui;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.entity.StaticDataEntity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 协议页面
 */
public class AgreementActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.webView)
    WebView mWebView;
    @BindView(R.id.view_network_error)
    View view_network_error;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agreement);
        ButterKnife.bind(this);
        initView();
        initData();
        initEvent();

    }



    @Override
    protected void initView() {
        tv_title.setText(R.string.text_agreement);
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
        if (!isNetWorkConnected(this)) {
            hindContent();
        }
    }

    @Override
    protected void initData() {
        StaticDataEntity mStaticDataEntity = SPUtil.getStaticData();
        if(mStaticDataEntity == null || mStaticDataEntity.getData().getAgreement_url() == null){
            return;
        }
        mWebView.loadUrl(mStaticDataEntity.getData().getAgreement_url());
    }

    @Override
    protected void initEvent() {
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    showContent();
                }
            }
        });
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    @OnClick(R.id.view_network_error)
    void netWorkError() {
        if(!isNetWorkConnected(this))
            return;
        initData();
    }


    private void showContent() {
        view_network_error.setVisibility(View.GONE);
        mWebView.setVisibility(View.VISIBLE);
    }

    private void hindContent() {
        view_network_error.setVisibility(View.VISIBLE);
        mWebView.setVisibility(View.GONE);
    }
}
